<template>
  <el-dialog
    :model-value="visible"
    @update:model-value="handleClose"
    title="投递详情"
    width="800px"
    :close-on-click-modal="false"
  >
    <div v-if="applicationData" class="apply-detail">
      <!-- 头部信息 -->
      <div class="detail-header">
        <div class="job-info">
          <h2>{{ applicationData.job?.jobName || '未知岗位' }}</h2>
          <p class="company-name">{{ applicationData.job?.enterprise?.enterpriseName || '未知公司' }}</p>
        </div>
        <div class="status-info">
          <el-tag :type="getStatusType(applicationData.status)" size="large">
            {{ getStatusText(applicationData.status) }}
          </el-tag>
        </div>
      </div>

      <!-- 投递信息卡片 -->
      <div class="detail-content">
        <el-row :gutter="20">
          <!-- 基本信息 -->
          <el-col :span="12">
            <el-card class="info-card" shadow="hover">
              <template #header>
                <div class="card-header">
                  <el-icon><User /></el-icon>
                  <span>基本信息</span>
                </div>
              </template>
              <div class="info-item">
                <label>投递人：</label>
                <span>{{ applicationData.userName }}</span>
              </div>
              <div class="info-item">
                <label>手机号：</label>
                <span>{{ applicationData.phone }}</span>
              </div>
              <div class="info-item">
                <label>邮箱：</label>
                <span>{{ applicationData.email || '未填写' }}</span>
              </div>
              <div class="info-item">
                <label>投递时间：</label>
                <span>{{ formatDate(applicationData.applyTime) }}</span>
              </div>
            </el-card>
          </el-col>

          <!-- 岗位信息 -->
          <el-col :span="12">
            <el-card class="info-card" shadow="hover">
              <template #header>
                <div class="card-header">
                  <el-icon><Briefcase /></el-icon>
                  <span>岗位信息</span>
                </div>
              </template>
              <div class="info-item">
                <label>工作地点：</label>
                <span>{{ applicationData.job?.location || '未填写' }}</span>
              </div>
              <div class="info-item">
                <label>薪资范围：</label>
                <span>{{ applicationData.job?.salary || '面议' }}</span>
              </div>
              <div class="info-item">
                <label>工作经验：</label>
                <span>{{ applicationData.job?.experience || '不限' }}</span>
              </div>
              <div class="info-item">
                <label>学历要求：</label>
                <span>{{ applicationData.job?.education || '不限' }}</span>
              </div>
            </el-card>
          </el-col>
        </el-row>

        <!-- 求职信 -->
        <el-card v-if="applicationData.coverLetter" class="cover-letter-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <el-icon><Edit /></el-icon>
              <span>求职信</span>
            </div>
          </template>
          <div class="cover-letter-content">
            {{ applicationData.coverLetter }}
          </div>
        </el-card>

        <!-- 简历信息 -->
        <el-card v-if="applicationData.resumeId || applicationData.resumeUrl" class="resume-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <el-icon><Document /></el-icon>
              <span>简历信息</span>
            </div>
          </template>
          <div class="resume-content">
            <div class="info-item" v-if="applicationData.resumeName">
              <label>简历名称：</label>
              <span>{{ applicationData.resumeName }}</span>
            </div>
            <div class="resume-actions">
              <el-button 
                v-if="applicationData.resumeId" 
                type="primary" 
                :icon="View" 
                @click="viewResume"
              >
                查看简历
              </el-button>
              <el-button 
                v-if="applicationData.resumeUrl" 
                type="info" 
                :icon="Download" 
                @click="downloadResume"
              >
                下载简历
              </el-button>
            </div>
          </div>
        </el-card>

        <!-- 面试信息 -->
        <el-card v-if="applicationData.interviewTime" class="interview-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <el-icon><Calendar /></el-icon>
              <span>面试信息</span>
            </div>
          </template>
          <div class="interview-content">
            <div class="info-item">
              <label>面试时间：</label>
              <span>{{ formatDate(applicationData.interviewTime) }}</span>
            </div>
            <div v-if="applicationData.hireTime" class="info-item">
              <label>录用时间：</label>
              <span>{{ formatDate(applicationData.hireTime) }}</span>
            </div>
          </div>
        </el-card>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { computed, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { 
  User, 
  Briefcase, 
  Edit, 
  Document, 
  Download, 
  View, 
  Calendar
} from '@element-plus/icons-vue'

const router = useRouter()

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  applicationData: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['update:visible', 'withdraw'])

const handleClose = () => {
  emit('update:visible', false)
}

// 查看简历
const viewResume = () => {
  if (props.applicationData.resumeId) {
    // 跳转到简历查看页
    const resumeId = props.applicationData.resumeId
    router.push(`/resume/${resumeId}`)
    // 关闭对话框
    handleClose()
  } else {
    ElMessage.warning('该投递没有关联的简历')
  }
}

const getStatusType = (status) => {
  const statusMap = {
    pending: 'warning',
    interviewing: 'primary',
    hired: 'success',
    rejected: 'danger'
  }
  return statusMap[status] || 'info'
}

const getStatusText = (status) => {
  const statusMap = {
    pending: '待处理',
    interviewing: '面试中',
    hired: '已录用',
    rejected: '已拒绝'
  }
  return statusMap[status] || '未知'
}


const formatDate = (dateString) => {
  if (!dateString) return '未设置'
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

const downloadResume = () => {
  if (props.applicationData.resumeUrl) {
    const link = document.createElement('a')
    link.href = props.applicationData.resumeUrl
    link.download = `简历_${props.applicationData.userName}.pdf`
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  }
}
</script>

<style scoped>
.apply-detail {
  padding: 0;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
  padding: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
}

.job-info h2 {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
}

.company-name {
  margin: 0;
  font-size: 16px;
  opacity: 0.9;
}

.status-info {
  text-align: right;
}

.progress-info {
  margin-top: 12px;
}

.progress-label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  opacity: 0.9;
}

.detail-content {
  margin-top: 20px;
}

.info-card,
.cover-letter-card,
.resume-card,
.interview-card {
  margin-bottom: 20px;
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: #333;
}

.info-item {
  display: flex;
  margin-bottom: 12px;
  align-items: center;
}

.info-item:last-child {
  margin-bottom: 0;
}

.info-item label {
  min-width: 80px;
  font-weight: 500;
  color: #666;
  margin-right: 12px;
}

.info-item span {
  color: #333;
  flex: 1;
}

.cover-letter-content {
  background: #f8f9fa;
  padding: 16px;
  border-radius: 8px;
  line-height: 1.6;
  color: #555;
  white-space: pre-wrap;
  border-left: 4px solid #409eff;
}

.resume-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.resume-actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.interview-content {
  background: #f0f9ff;
  padding: 16px;
  border-radius: 8px;
  border-left: 4px solid #67c23a;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .detail-header {
    flex-direction: column;
    gap: 16px;
  }
  
  .status-info {
    text-align: left;
  }
  
  .resume-content {
    flex-direction: column;
  }
}
</style>

















